package day_2022_12_to_3.year2023;

/**
 * @author haomin
 * @date 2023/01/07 23:26
 **/
public class Day1_7_Review {
    public class Solution15 {
        // you need to treat n as an unsigned value
        // 1 & 1 = 1
        // 1 & 0 = 0
        // 0 & 1 = 0
        // 0 & 0 = 0
        public int hammingWeight(int n) {
            int count = 32;
            int ans = 0;
            while(count-- > 0) {
                ans += n & 1;
                n >>= 1;
            }
            return ans;
        }
    }

    class Solution17 {
        public int[] printNumbers(int n) {
            int len = (int)Math.pow(10, n) - 1;
            int[] arr = new int[len];
            for(int i = 0; i < len; ++i) {
                arr[i] = i + 1;
            }
            return arr;
        }
    }

    class Solution18 {
        public ListNode deleteNode1(ListNode head, int val) {
            if(head == null) return head;
            ListNode dummyHead = new ListNode(-1);
            dummyHead.next = head;
            ListNode cur = dummyHead;
            while(cur.next.val != val) cur = cur.next;
            cur.next = cur.next.next;
            return dummyHead.next;
        }

        // 方法二：递归
        public ListNode deleteNode(ListNode head, int val) {
            if(head == null) return head;
            if(head.val == val) return head.next;
            head.next = deleteNode(head.next, val);
            return head;
        }
    }

    class Solution20 {
        public boolean isNumber(String s) {
            s = s.trim();
            char[] arr = s.toCharArray();
            boolean isNum = false, isFloat = false, isE = false;
            for(int i = 0; i < arr.length; ++i){
                char c = arr[i];
                if((c == '+' || c == '-') &&
                        ((i == 0) || ((i > 0) && (arr[i-1] == 'e' || arr[i-1] == 'E')))){
                }else if((c == 'e' || c == 'E') && !isE && isNum){
                    isNum = false;
                    isE = true;
                }else if(c == '.' && !isFloat && !isE){
                    isFloat = true;
                }else if(Character.isDigit(c)){
                    isNum = true;
                }else {
                    return false;
                }
            }
            return isNum;
        }
    }
}